//
//  EFengComposeEmotionScrollView.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/3/7.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "EFengComposeEmotionScrollView.h"
#import "MicroDef.h"
#import "EMotionButton.h"
#import "CHEmotionHelper.h"

#define EdgeSpaceLeft   10
#define EmotionWidth (HARDWARE_SCREEN_WIDTH - 20) / 8.0f


static EFengComposeEmotionScrollView* shared;
@implementation EFengComposeEmotionScrollView


- (void)dealloc
{
    //    NSLog(@"EmotionScroll dealloc");
}

+ (EFengComposeEmotionScrollView*)defaultComposeEmoScroll
{
    @synchronized(self)
    {
        if(shared == nil)
        {
            shared = [[EFengComposeEmotionScrollView alloc] init];
        }
    }
    return shared;
}

+ (EFengComposeEmotionScrollView *)defaultComposeEmoScrollWithDelegate:(id)sender
{
    @synchronized(self)
    {
        if(shared == nil)
        {
            shared = [[EFengComposeEmotionScrollView alloc] init];
            shared.emoDelegate = sender;
        }
    }
    return shared;
}

- (id)init
{
    self = [super init];
    if (self) {
        [self setProperty];
    }
    return self;
}

-(void)setProperty
{
    self.bounces = NO;
    self.scrollEnabled = YES;
    self.pagingEnabled = YES;
    self.alwaysBounceHorizontal = YES;
    self.alwaysBounceVertical = NO;
    self.showsHorizontalScrollIndicator = NO;
    self.showsVerticalScrollIndicator = NO;
    
}

- (void)setEmotionArray:(NSArray *)emotionArray
{
    _emotionArray = emotionArray;
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    NSMutableArray *tempEmotion = [NSMutableArray arrayWithArray:emotionArray];
    
    NSInteger pages = [tempEmotion count]%23 ? [emotionArray count]/23 + 1 : [tempEmotion count]/23;
    
    for (int i = 0; i < pages; i ++) {
        EMotionModel *emo = [[EMotionModel alloc] init];
        emo.png = @"deleteBtn";
        emo.chs = @"";
        if (i != pages -1) {
            [tempEmotion insertObject:emo atIndex:((i + 1) * 24 - 1)];
        } else {
            [tempEmotion addObject:emo];
        }
        emo = nil;
    }
    
    [self setContentSize:CGSizeMake(pages*HARDWARE_SCREEN_WIDTH, 40)];
    
    for (int i=0; i<tempEmotion.count; i++) {
        EMotionModel *model = tempEmotion[i];
        
        if ([model.png isEqualToString:@"deleteBtn"]) {
            EMotionButton *delete = [self buttonWithNormalImg:[UIImage imageNamed:@"emotion_delete"] selectedImg:[UIImage imageNamed:@"emotion_delete"] action:@selector(deleteEmotionAction)];
            delete.emotionModel = model;
            [delete setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [self addSubview:delete];
        }else{
            NSString *path = [[NSBundle mainBundle] pathForResource:model.png ofType:nil inDirectory:model.directory];
            EMotionButton *btn = [self buttonWithNormalImg:[UIImage imageWithContentsOfFile:path] selectedImg:[UIImage imageWithContentsOfFile:path] action:@selector(buttonClick:)];
//            EMotionButton* btn = [self buttonWithNormalImg:[UIImage imageNamed:[NSString stringWithFormat:@"%@/%@",model.directory,model.png]] selectedImg:[UIImage imageNamed:[NSString stringWithFormat:@"%@/%@",model.directory,model.png]] action:@selector(buttonClick:)];
            btn.emotionModel = model;
            [self addSubview:btn];
        }
    }
    [self setNeedsLayout];
    
}

- (EMotionButton *)buttonWithNormalImg:(UIImage*)normalImg selectedImg:(UIImage*)selectedImg action:(SEL)sel
{
    
    EMotionButton *btn = [EMotionButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:normalImg forState:UIControlStateNormal];
    [btn setImage:selectedImg forState:UIControlStateSelected];
    [btn addTarget:nil action:sel forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    
    int page = 0;
    int line = 0;
    int posX = 0;
    int i = 0;
    for (EMotionButton *btn in self.subviews) {
        page = i/24;//i/24;
        line = i/8%3;
        posX = i%8;
        btn.frame = CGRectMake(posX*EmotionWidth+page*HARDWARE_SCREEN_WIDTH+EdgeSpaceLeft, line*40, EmotionWidth, 40);
        i ++;
        
    }
    
}


- (void)buttonClick:(EMotionButton *)sender
{
    //    NSLog(@"%d",btn.tag);
    
    //    NSLog(@"%@",str);
    
    //只针对CommentToolBar实现参数传递
    if (_emoDelegate && [_emoDelegate respondsToSelector:@selector(emotionButtonSelectedWith:)]) {
        [_emoDelegate emotionButtonSelectedWith:sender.emotionModel.chs];
    }
    
    if (sender.emotionModel == nil) {
        return;
    }
    [CHEmotionHelper addRecentEmotion:sender.emotionModel];
    //聊天中用通知实现参数传递
    [[NSNotificationCenter defaultCenter] postNotificationName:composeSendTagOfEmotionIcon object:sender.emotionModel.chs];
    
    //    [self.emoDelegate emotionButtonDidSelected:self withIndex:btn.tag-1000 nameStr:str];
}

//表情删除
- (void)deleteEmotionAction{
    if (_emoDelegate && [_emoDelegate respondsToSelector:@selector(emotionButtonDelete)]) {
        [_emoDelegate emotionButtonDelete];
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:composeDeleteTagOfEmotionIcon object:nil];
}


@end
